package sg.edu.nus.iss.billing.parse;

import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
/**
 * @author sammil
 *
 */
public class SubscriptionHeaderParser extends Parser {
	
	public static final String PH_NO_LABEL = "#PHONE NUMBER:";
	public static final int PH_NO_LENGTH = 16;
	
	public static final String SERVICE_TYPE_LABEL = "SERVICE TYPE:";
	public static final int SERVICE_TYPE_LENGTH = 15;
	
	public static final String CALL_TYPE_LABEL = "CALLTYPE:";
	public static final int CALL_TYPE_LENGTH = 10;
	
	public static final int MAX_LENGTH = 1 * Parser.MAX_LINE_LEN;
	
	static String phoneNo = null; 
	static String serviceType = null;
	static String callType = null;
	
	private SubscriptionHeaderParser() {
	}

	public static final SubscriptionHeaderParser SINGLETON = new SubscriptionHeaderParser();
	
	@Override
	public <L> boolean parse(LineNumberReader in, HashMap<String,ArrayList<L>> outputMap)
			throws IOException {
		try {

			// remember where we started from
			in.mark(MAX_LENGTH);
				
			if (!consume(in, PH_NO_LABEL) ) {
				
				System.out.println("Failed to Parse Subscription Header ");
				return false;
			}

			in.reset();
			StringReader line = nextLine(in);
			// Read the first line
				
			String accountNo = AccountHeaderParser.accountNo;
			phoneNo = readToken(PH_NO_LABEL, line, PH_NO_LENGTH).trim();
			serviceType = readToken(SERVICE_TYPE_LABEL, line, SERVICE_TYPE_LENGTH).trim();
			callType = readToken(CALL_TYPE_LABEL, line, CALL_TYPE_LENGTH).trim();
			System.out.println("accountNo[" +  accountNo+"] phoneNo[" +  phoneNo+"] serviceType[" +  serviceType+"] callType[" +  callType+"]");
			
			
		} catch (IOException e) {

			System.err.println("Problem in SubscriptionHeaderParser :" +  e);
			// go back to where we started
			in.reset();
			// re-throw the exception
			throw e;
		}
		System.out.println("Parsed  SubscriptionHeader record");
		return true;
	}

}
